<%@ page import="com.online_video.entity.Video" %>
<%@ page import="com.online_video.entity.Page" %>
<%@ page import="com.online_video.entity.User" %>
<%@ page import="com.online_video.entity.VideoPage" %><%--
  Created by IntelliJ IDEA.
  User: 渣全蛋
  Date: 2022/3/16
  Time: 19:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>视频管理</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/titatoggle/2.1.2/titatoggle-dist-min.css" rel="stylesheet">
    <script type="text/javascript" src="../js/jquery-1.11.3.min.js"></script>
    <link rel="stylesheet" href="../js/bootstrap-3.4.1-dist/css/bootstrap.min.css">
    <script type="text/javascript" src="../js/bootstrap-3.4.1-dist/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../js/sweetalert.min.js"></script>
    <%
        VideoPage<Video> videoPage = (VideoPage<Video>) request.getAttribute("video");
    %>
</head>
<body>
<%@include file="plugins/navbar.jsp" %>
<br><br><br>
<h2 class="sub-header">视频管理</h2>
<div class="table-responsive">
    <table class="table table-striped">
        <thead>
        <tr>
            <th>编号</th>
            <th>视频封面</th>
            <th>视频名称</th>
            <th>视频类型</th>
            <th>上传者</th>
            <th>视频描述</th>
            <th>上传日期</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>

        <%
            if (videoPage == null) {
                response.sendRedirect("./admin.do?action=videoPage");
            } else {
                for (int i = 0; i < videoPage.getItems().size(); i++) {
                    Video video = videoPage.getItems().get(i);
        %>
        <tr>
            <td><%=video.getVid()%>
            </td>
            <td>
                <img class="img-thumbnail" src="/online_video/img/<%=video.getImg()%>"
                     style="max-width: 160px;height: 80px"/>
            </td>
            <td><%=video.getViName()%>
            </td>
            <td><%=video.getCategory()%>
            </td>
            <td class="<%=video.getU_id()%>">
                <script>
                    /*进行一次调用才执行js*/
                    getName()
                    function getName(){
                       $.ajax({
                           dataType: "text",
                           method: "POST",
                           url: './admin.do?action=queryByid',
                           async: true,
                           data:{id:<%=video.getU_id()%>},
                           success:(data)=>{
                               if (data!=null&&data!=""){
                                   var user = JSON.parse(data);
                                 /*  $("td:eq(4)").val(user.username);*/
                                   console.log(user.username);
                                    name = user.username;

                                   $(".<%=video.getU_id()%>").text(user.username)

                               }else {
                                   $(".<%=video.getU_id()%>").text("该用户信息已注销")
                               }
                           }
                       })
                   }
                </script>

            </td>
            <td><%=video.getDescribe()%>
            </td>
            <td><%=video.getDate()%>
            </td>
            <td>
                <button class="btn btn-danger" onclick="deleteVideo($(this).parents('tr').children('td:eq(0)').text().trim())">下架视频</button>
            </td>
        </tr>
        <%
                }
            }
        %>
        </tbody>
    </table>
    <nav aria-label="Page navigation" style="margin-left: 40%">
        <ul class="pagination">
            <%
                if (videoPage != null) {
                    if (videoPage.getPageNo() > 1) {
            %>
            <li>
                <a href="./admin.do?action=videoPage&pageNo=1&pageSize=<%=videoPage.getPageSize()%>">首页</a>
            </li>
            <li>
                <a href="./admin.do?action=videoPage&pageNo=<%=videoPage.getPageNo()-1%>"
                   aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            <%
                }
                int beginPageNo = 0;
                int endPageNo = 0;
                //情况1：如果总页码小于等于5 的情况，页码的范围是：1-总页码
                if (videoPage.getPageTotal() <= 5) {
                    beginPageNo = 1;
                    endPageNo = videoPage.getPageTotal();
                } else {
                    //情况2：总页码大于5 的情况
                    if (videoPage.getPageNo() <= 3) {
                        //小情况1：当前页码为前面3 个：1，2，3 的情况，页码范围是：1-5
                        beginPageNo = 1;
                        endPageNo = 5;
                    } else if (videoPage.getPageNo() >= (videoPage.getPageTotal() - 2)) {
                        //小情况2：当前页码为最后3 个，8，9，10，页码范围是：总页码减4 - 总页码
                        beginPageNo = videoPage.getPageTotal() - 4;
                        endPageNo = videoPage.getPageTotal();
                    } else {
                        //小情况3：4，5，6，7，页码范围是：当前页码减2 - 当前页码加2
                        beginPageNo = videoPage.getPageNo() - 2;
                        endPageNo = videoPage.getPageNo() + 3;
                    }
                }
                for (int i = beginPageNo; i <= endPageNo; i++) {
                    if (i == videoPage.getPageNo()) {
            %>
            <li>
                <a href="./admin.do?action=videoPage&pageNo=<%=i%>&pageSize=<%=videoPage.getPageSize()%>"><<%=i%>>
                </a>
            </li>
            <%
            } else {
            %>
            <li>
                <a href="./admin.do?action=videoPage&pageNo=<%=i%>&pageSize=<%=videoPage.getPageSize()%>"><<%=i%>>
                </a>
            </li>
            <%
                    }
                }
                if (videoPage.getPageNo() < videoPage.getPageTotal()) {
            %>
            <li>
                <a href="./admin.do?action=videoPage&pageNo=<%=videoPage.getPageNo()+1%>" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
            <li>
                <a href="./admin.do?action=videoPage&pageNo=<%=videoPage.getPageTotal()%>&pageSize=<%=videoPage.getPageSize()%>">末页</a>
            </li>

            <%
                    }
                }
            %>

        </ul>
    </nav>
</div>
<script>
    window.jQuery || document.write(
        '<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/vendor/jquery.min.js"><\/script>'
    )
</script>
<script>
    function deleteVideo(vid) {
        swal({
            title: "确认删除",
            text: "是否确认删除改条记录",
            icon: "warning",
            buttons: {
                catch: {
                    text: "确认",
                    value: true,
                },
                cancel: "取消"
            }
        }).then((isOk)=> {
            console.log(isOk);
            if (isOk) {
                $.ajax({
                    dataType: "text",
                    method: "POST",
                    url: '../client/video.do?action=delVideoByVid',
                    async: true,
                    data:{
                        vid:vid,
                    },success:(data)=>{
                        if (data!=null&&data!=""){
                            var parse = JSON.parse(data);
                            if (parse===1){
                                swal("删除成功").then((isContinue)=>{
                                    if (isContinue){
                                        window.location.reload();
                                    }
                                })
                            }
                        }
                    }
                })
            }
        });
    }
</script>
</body>
</html>